package io.gitee.xyuesheng.day05;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class TestList {
    @Test
    public void test2() {
        List list = new ArrayList();
        String s = "abc";
        list.add(s);
        list.add(s);
        System.out.println(list);
    }

    @Test
    public void test4() {
        List<String> list = new ArrayList<>();
        list.add("abc");
        list.add("def");
        list.add("abc");
        System.out.println(list);
        list.remove("abc");
        System.out.println(list);
    }

    //老师没有实现eauals方法，remove（）删除不了
    @Test
    public void test5() {
        List<Teacher> teachers = new ArrayList<>();

        teachers.add(new Teacher("黄老师"));
        teachers.add(new Teacher("付老师"));
        System.out.println(teachers);
        teachers.remove(new Teacher("黄老师"));
        System.out.println(teachers);
    }

    @Test
    public void test6() {
        List<Student> students = new ArrayList<>();
        students.add(new Student("张三", 18, "1001"));
        students.add(new Student("李四", 18, "1001"));
        System.out.println(students);
        students.remove(new Student("张三", 18, "1001"));
        System.out.println(students);

        if (students.contains(new Student("李四", 18, "1001"))) {
            System.out.println("有...");
        }
    }

    @Test
    public void testIterator() {
        List<String> list = new ArrayList<>();

        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");
        list.add("zhaoliu");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        //增强的for循环，foreach
        for (String s : list) {
            System.out.println(s);
        }
        //迭代器模式
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();//指针指向下一位
            System.out.println(next);
        }
    }

    @Test
    //遍历时删除某个元素
    public void testRemove() {
        List<String> list = new ArrayList<>();

        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");
        list.add("zhaoliu");
//        for(String s:list){
//            if("wangwu".equals(s)){
//                list.remove(s);//删除第一个最后一个会报错，随机的
//            }
//        }
//        System.out.println(list);

        Iterator<String> iter = list.iterator();
        while (iter.hasNext()) {
            String next = iter.next();
            if ("wangwu".equals(next)) {
                iter.remove();//删除当前位置处的元素
            }
        }
        System.out.println(list);

    }

    //泛型，指定容器里面元素的类型
    @Test
    public void test3() {
        List<String> list = new ArrayList<>();
        list.add("abc");
        list.add("def");
        List<Integer> list2 = new ArrayList<>();
    }


    @Test
    public void test() {
        Assert.assertArrayEquals(new int[]{2}, findprimerIn(2));
        Assert.assertArrayEquals(new int[]{2, 3}, findprimerIn(3));
        Assert.assertArrayEquals(new int[]{2, 3, 5, 7}, findprimerIn(10));
        findprimerIn(100);
    }

    @Test
    public void testList() {
        List list = findPrimeResultList(100);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

    public List findPrimeResultList(int max) {
        if (max < 2) {
            return null;
        }
        List list = new ArrayList();
        for (int i = 2; i < max; i++) {
            if (isPrime(i)) {
                list.add(i);
            }
        }
        return list;
    }


    public int[] findprimerIn(int max) {
//        for(int i=3;i<=max;i++){
//            boolean flag=false;
//            for(int j=2;j<i;j++){
//                if(i%j==0){
//                    false=true;
//                    break;
//                }
//            }
//            if (flag) {
//                System.out.println();
//            }
//        }
//        return null;

        if (max < 2) {
            return new int[]{};
        }
        int[] result = new int[max];
        int index = 0;
//        result[index]=2;
//        index++;
        for (int i = 2; i <= max; i++) {
            boolean flag = isPrime(i);
            if (isPrime(i)) {
                result[index] = i;
                index++;
            }
        }
        return Arrays.copyOf(result, index);

    }

    //判断参数是否是质数
    public boolean isPrime(int num) {
        for (int i = 2; i < num; i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }


}
